package com.yzpass.api.common;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;

import java.util.ArrayList;
import java.util.List;

/**
 * @author junfeng
 */
public class OffsetPage<T> implements IPage<T> {
    List<T> list;
    long total;
    long size;
    long current;
    long offset;
    List<OrderItem> orderItems;

    boolean searchCount = true;

    @Override
    public boolean optimizeCountSql() {
        return searchCount;
    }

    @Override
    public boolean searchCount() {
        return searchCount;
    }

    public OffsetPage(long offset, long size){
        this.offset = offset;
        this.size = size;
        this.current = 1 + offset/size;
        orderItems = new ArrayList<>();
    }

    public OffsetPage(long offset, long size,boolean searchCount){
        this.offset = offset;
        this.size = size;
        this.current = 1 + offset/size;
        this.searchCount = searchCount;
        orderItems = new ArrayList<>();
    }


    @Override
    public List<OrderItem> orders() {
        return orderItems;
    }

    @Override
    public List<T> getRecords() {
        return list;
    }

    @Override
    public IPage<T> setRecords(List<T> records) {
        list = records;
        return this;
    }

    @Override
    public long getTotal() {
        return total;
    }

    @Override
    public IPage<T> setTotal(long total) {
        this.total = total;
        return this;
    }

    @Override
    public long getSize() {
        return size;
    }

    @Override
    public IPage<T> setSize(long size) {
        this.size = size;
        return this;
    }

    @Override
    public long getCurrent() {
        return current;
    }

    @Override
    public IPage<T> setCurrent(long current) {
        this.current = current;
        this.offset = current*size;
        return this;
    }

}
